package com.mmy.controller.sys;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.sys.HeaderServiceImpl;

import lombok.extern.slf4j.Slf4j;

@Controller
@Slf4j
public class HeaderController {

	@Autowired
	HeaderServiceImpl headerServiceImpl;

	/**
	 * 函数功能说明： 前台菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@RequestMapping("/loadfrontmenu")
	@ResponseBody
	public Map<String, Object> findFrontMenu(HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
		
		/* 用户为空默认菜单 */
		if (tSysUser == null) {
			Map<String, Object> data = headerServiceImpl.findMenu();
			log.info("默认菜单");
			return data;
		}
		/* 员工菜单 */
		if (tSysUser.getUserType() != 0) {
			Map<String, Object> data = headerServiceImpl.findFrontMenu();
			log.info("员工菜单");
			return data;

		} else {
			/* 游客为默认菜单 */
			Map<String, Object> data = headerServiceImpl.findMenu();
			log.info("游客菜单");
			return data;
		}

	}

	/**
	 * 函数功能说明： 后台菜单
	 *
	 * @param 参数说明
	 * @return 返回值说明
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */
	@PostMapping("/loadbackmenu")
	@ResponseBody
	public Map<String, Object> findBackMenu(HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		if (tSysUser.getUserAccount().equals("administrator") && tSysUser.getUserID() == 0) {
			Map<String, Object> data = headerServiceImpl.findBackMenu();
			return data;

		} else {
			int iUserID = tSysUser.getUserID();
			Map<String, Object> data = headerServiceImpl.findBackByID(iUserID);
			return data;
		}

	}

	@PostMapping("/sessioncity")
	@ResponseBody
	public void sessionCity(HttpServletRequest request, HttpSession session) {
		String cityName = request.getParameter("city");
		session.setAttribute("sessionCity", cityName);

	}
}
